package com.cqjtu.tto.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.tto.dao.mapper.PermissionsMapper;
import com.cqjtu.tto.dao.mapper.PermissionsRoleMapper;
import com.cqjtu.tto.dao.mapper.RoleMapper;
import com.cqjtu.tto.dao.pojo.Permissions;
import com.cqjtu.tto.dao.pojo.PermissionsRole;
import com.cqjtu.tto.dao.pojo.Role;
import com.cqjtu.tto.service.RoleService;
import com.cqjtu.tto.vo.ErrorCode;
import com.cqjtu.tto.vo.Results;
import com.cqjtu.tto.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author：monkey-L
 * @description:
 * @date: 18:47 2021/12/11
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionsMapper permissionsMapper;

    @Autowired
    private PermissionsRoleMapper permissionsRoleMapper;

    @Override
    @Transactional
    public Results roleAdd(String roleName) {
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,roleName).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role != null) {
            return Results.fail(ErrorCode.ROLE_EXIST.getCode(),ErrorCode.ROLE_EXIST.getMsg());
        }
        Role role1 = new Role();
        role1.setRoleId(System.currentTimeMillis());
        role1.setRoleName(roleName);
        role1.setRoleDeleted(0);
        roleMapper.insert(role1);
        return Results.success("添加角色成功！");
    }

    @Override
    @Transactional
    public Results roleDelete(String roleName) {
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,roleName).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(),ErrorCode.ROLE_NOT_EXIST.getMsg());
        }

        LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
        permissionsRoleWrapper.eq(PermissionsRole::getRoleId,role.getRoleId());
        List<PermissionsRole> permissionsRoles = permissionsRoleMapper.selectList(permissionsRoleWrapper);
        for (PermissionsRole permissionsRole : permissionsRoles) {
            LambdaQueryWrapper<PermissionsRole> permissionsRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            permissionsRoleLambdaQueryWrapper.eq(PermissionsRole::getAuthorityRoleId,permissionsRole.getAuthorityRoleId());
            permissionsRoleMapper.delete(permissionsRoleLambdaQueryWrapper);
        }
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.eq(Role::getRoleId,role.getRoleId());
        roleMapper.delete(roleLambdaQueryWrapper);
        return Results.success("删除角色成功！");
    }

    @Override
    @Transactional
    public Results roleUpdate(String oldRoleName, String newRoleName) {
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,oldRoleName).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(),ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        Role role1 = new Role();
        role1.setRoleId(role.getRoleId());
        role1.setRoleName(newRoleName);
        role1.setRoleDeleted(0);
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleLambdaQueryWrapper.eq(Role::getRoleId,role.getRoleId());
        roleMapper.update(role1,roleLambdaQueryWrapper);
        return Results.success("更新角色信息成功！");
    }

    @Override
    @Transactional
    public Results roleSelectOne(String roleName) {
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.eq(Role::getRoleName,roleName).last("limit 1");
        Role role = roleMapper.selectOne(roleWrapper);
        if (role == null) {
            return Results.fail(ErrorCode.ROLE_NOT_EXIST.getCode(),ErrorCode.ROLE_NOT_EXIST.getMsg());
        }
        LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
        permissionsRoleWrapper.eq(PermissionsRole::getRoleId,role.getRoleId());
        List<PermissionsRole> permissionsRoles = permissionsRoleMapper.selectList(permissionsRoleWrapper);
        RoleVo roleVo = new RoleVo();
        roleVo.setRoleName(role.getRoleName());
        ArrayList<String> list = new ArrayList<>();
        for (PermissionsRole permissionsRole : permissionsRoles) {
            LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
            permissionsWrapper.eq(Permissions::getPermissionsId,permissionsRole.getPermissionsId()).last("limit 1");
            Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
            list.add(permissions.getPermissionsName());
        }
        roleVo.setPermissionsName(list);
        return Results.success(roleVo);
    }

    @Override
    @Transactional
    public Results roleSelectList() {
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        List<Role> roles = roleMapper.selectList(roleWrapper);
        if (roles == null) {
            return Results.success("角色信息为空");
        }
        List<RoleVo> roleVos = new ArrayList<>();
        for (Role role : roles) {
            LambdaQueryWrapper<PermissionsRole> permissionsRoleWrapper = new LambdaQueryWrapper<>();
            permissionsRoleWrapper.eq(PermissionsRole::getRoleId,role.getRoleId());
            List<PermissionsRole> permissionsRoles = permissionsRoleMapper.selectList(permissionsRoleWrapper);
            RoleVo roleVo = new RoleVo();
            roleVo.setRoleName(role.getRoleName());
            ArrayList<String> list = new ArrayList<>();
            for (PermissionsRole permissionsRole : permissionsRoles) {
                LambdaQueryWrapper<Permissions> permissionsWrapper = new LambdaQueryWrapper<>();
                permissionsWrapper.eq(Permissions::getPermissionsId,permissionsRole.getPermissionsId()).last("limit 1");
                Permissions permissions = permissionsMapper.selectOne(permissionsWrapper);
                list.add(permissions.getPermissionsName());
            }
            roleVo.setPermissionsName(list);
            roleVos.add(roleVo);
        }
        return Results.success(roleVos);
    }
}
